package com.nbsystem.interceptor;

import com.nbsystem.entity.SysUser;
import com.nbsystem.util.Const;
import org.apache.ibatis.plugin.Intercepts;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String url = httpServletRequest.getRequestURI();
        if (url.indexOf("login") >= 0 || url.contains("index")) {
            return true;
        }
        HttpSession session = httpServletRequest.getSession();
        SysUser sysUser = (SysUser) session.getAttribute(Const.USERSESSION);
        if (sysUser != null){
            return true;
        }
       // httpServletRequest.setAttribute("msg","你还没有登录，请先登录");
        //httpServletRequest.getRequestDispatcher("/WEB-INF/index.jsp").forward(httpServletRequest,httpServletResponse);
        httpServletResponse.sendRedirect("index");
        return false;
    }
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
